require 'sinatra'
require 'haml'
require 'sass'
require 'coffee-script'
require 'mongoid'
require "mongoid_auto_increment_id"
require 'rack-flash'
require 'sinatra/redirect_with_flash'
set :public_folder,File.dirname(__FILE__) + '/static'
set :views,File.dirname(__FILE__) + '/tpl'
set :haml, {:format => :html5 }
env = ENV['RACK_ENV'] || :development
Mongoid.load!("./mongoid.yml",env)
enable :sessions
use Rack::Flash
class Task
    include Mongoid::Document
    field :title, type: String
    field :body, type: String
end
get '/' do
    @tasks= Task.all
    haml :index
end
get '/new' do
    t= Task.new
    haml :new
end
post '/new' do
    t=Task.new(params[:task])
    if t.save
        redirect '/' ,:notice => "Logged in"
    else
        "Error saveing doc"
    end
end
get '/post/:id' do |id|
    @task=Task.find(id)
    haml :post
end
get '/edit/:id' do |id|
    @task=Task.find(id)
    haml :edit
end
put '/update/:id' do
    @task=Task.find(params[:id])
    @task.update_attributes(params[:task])
    redirect '/post/'+params[:id]
end
delete '/delete/:id' do
    @task=Task.find(params[:id])
    if @task.delete
        p 'delete ok'
    else
        p 'delete error'
    end
end
get '/style.css' do
    scss :style
end
get '/my.js' do
    coffee :my
end
